package com.binvshe.binvshe.contacts;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.binvshe.binvshe.mine.view.CommentDialog;
import com.pachong.android.frameworkbase.utils.GlideHelper;

import java.util.List;

/**
 * 添加好友界面adapter
 * 获取手机联系人，并且判断是否可加好友
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016-10-28
 * @Version: 1.0.0
 */
public class FriendContactAdapter extends SearchResultAdapter {

    public FriendContactAdapter(Context con) {
        super(con);
    }

    List<ContactsService.Contact> contacts;

    public void setPhoneContacts(List<ContactsService.Contact> contacts) {
        this.contacts = contacts;
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        final ContactBean contact = (ContactBean) getData().get(position);

        viewHolder.tvName.setText(contact.getNick_name());
        //通讯录姓名
        viewHolder.tvContent.setText(contacts.get(position).name);
        GlideHelper.loadImageWithRound(getContext(), viewHolder.ivPortraid, contact.getPortrait());
        viewHolder.button.setVisibility(View.VISIBLE);
        if (contact.getApply_state() == ContactBean.STATE_NOT_REQ) {
            viewHolder.button.setText("加好友");
            viewHolder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (getContext() instanceof AppCompatActivity) {
                        AppCompatActivity activity = (AppCompatActivity) getContext();

                        final CommentDialog commentDialog = CommentDialog.newInstance("申请理由", "请输入申请理由", 4, true);
                        commentDialog.show(((AppCompatActivity) getContext()).getSupportFragmentManager(), "dd");
                        commentDialog.setOnClickable(new CommentDialog.CommentClickable() {

                            @Override
                            public void btnEnter(final String comment) {
                                requestAddFriend(""+contact.getUser_id(), comment);
                                commentDialog.dismiss();
                            }
                        });

                      /*  final EditDialogFragment reasonDialog = new EditDialogFragment();
                        reasonDialog.setOnCententSavedListener(new EditDialogFragment.OnContentSavedListener() {
                            @Override
                            public void OnContentSaved(CharSequence content) {
                                requestAddFriend(""+contact.getUser_id(), content.toString());
                                reasonDialog.dismiss();
                            }
                        })
                                .setTitle("请输入申请理由")
                                .show(activity.getSupportFragmentManager());*/

                    }
                }
            });
        } else if (contact.getApply_state() == ContactBean.STATE_REQUESTING) {
            viewHolder.button.setText("已发送");
            viewHolder.button.setBackgroundDrawable(null);
            viewHolder.button.setTextColor(Color.parseColor("#333333"));
        }


    }

    public class ContactBean {

        /**
         * user_id : 100561
         * login_name : 18959232125
         * nick_name : 马新煌
         * gender : 1
         * portrait : http://static.binvshe.com/static/smart_ring/20161027170339095819_750*750.jpg
         * name_py : maxinhuang
         * initial : M
         * contact_user_name : 马新煌
         * apply_state : -1
         */

        // apply_state -1 未申请 0 申请中 1 已为好友
        public static final int STATE_NOT_REQ = -1;
        public static final int STATE_REQUESTING = 0;
        public static final int STATE_FRIEND = 1;


        private int user_id;
        private String login_name;
        private String nick_name;
        private int gender;
        private String portrait;
        private String name_py;
        private String initial;
        private String contact_user_name;
        private int apply_state;

        public int getUser_id() {
            return user_id;
        }

        public void setUser_id(int user_id) {
            this.user_id = user_id;
        }

        public String getLogin_name() {
            return login_name;
        }

        public void setLogin_name(String login_name) {
            this.login_name = login_name;
        }

        public String getNick_name() {
            return nick_name;
        }

        public void setNick_name(String nick_name) {
            this.nick_name = nick_name;
        }

        public int getGender() {
            return gender;
        }

        public void setGender(int gender) {
            this.gender = gender;
        }

        public String getPortrait() {
            return portrait;
        }

        public void setPortrait(String portrait) {
            this.portrait = portrait;
        }

        public String getName_py() {
            return name_py;
        }

        public void setName_py(String name_py) {
            this.name_py = name_py;
        }

        public String getInitial() {
            return initial;
        }

        public void setInitial(String initial) {
            this.initial = initial;
        }

        public String getContact_user_name() {
            return contact_user_name;
        }

        public void setContact_user_name(String contact_user_name) {
            this.contact_user_name = contact_user_name;
        }

        public int getApply_state() {
            return apply_state;
        }

        public void setApply_state(int apply_state) {
            this.apply_state = apply_state;
        }
    }

}
